package com.sunday.demo.exception;

import com.sunday.demo.idGenerator.IdGenerator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IdGeneratorException
 * @Author JiangHai
 * @Date 2021/9/4 20:24
 **/
public class IdGeneratorException extends RuntimeException{
    protected String code;
    protected String msg;

    public IdGeneratorException(String code, String msg) {
        super(msg);
        this.msg = msg;
    }

    private static void doCalculateAmount(List<Person> list,BigDecimal amount) {
        int size = list.size();

        BigDecimal temp = BigDecimal.ZERO;
        for (int i = 0; i < size; i++) {
            Person person = list.get(i);
            int j = size - i;
            BigDecimal priceTemp = amount.subtract(temp);
            if (i == size - 1) {
                person.setPrice(amount.subtract(temp));
            }else {
                person.setPrice(priceTemp.divide(new BigDecimal(j), 2, RoundingMode.HALF_UP));
                temp = temp.add(person.getPrice());
            }
        }

        int count = 0;
        for (Person person : list) {
            int j = size - count;
            BigDecimal pr = amount.subtract(temp);
            if (count == size - 1) {
                person.setPrice(amount.subtract(temp));
            }else {
                person.setPrice(pr.divide(new BigDecimal(j), 2, RoundingMode.HALF_UP));
                temp = temp.add(person.getPrice());
            }
            count++;
        }
    }

}


class Person{
    String name;
    BigDecimal price;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}